---
title: "Column formatting within Shell / Vim, e.g., for Neomutt and Abook"
date:  2019-01-16
category:  TechTips
---

The following is a micro-tip for formatting text in vim.  If you are writing
a document and want to have the text in neatly aligned columns, resembling
a table, you can pipe text to the shell's **column** command.

To do so, select a range of text with visual mode (Shift-V), and then enter the
following ex command:

`:!column -t`

So to demonstrate, you can go from having text that looks like this:

GNU Linux Parabola Gentoo Comfy Memes  
RC 1337 N3rd Ghanoo Wizard SuperDuperLongWord  
Apples Oranges Windoze 9000 Remainder Clutch  

to text that looks like this:

<table>
<tr>
<td>GNU</td> <td>Linux</td> <td>Parabola</td> <td>Gentoo</td> <td>Comfy</td> <td>Memes</td>
</tr>
<tr>
<td>RC</td> <td>1337</td> <td>N3rd</td> <td>Ghanoo</td> <td>Wizard</td> <td>SuperDuperLongWord</td>  
</tr>
<tr>
<td>Apples</td> <td>Oranges</td> <td>Windoze</td> <td>9000</td> <td>Remainder</td> <td>Clutch</td>  
</tr>
</table>

I like using this command for managing my [abook](http://abook.sourceforge.net/)
address book file that is used by [neomutt](https://www.neomutt.org).  To
illustrate: the raw text of the alias file, generated by calling abook from
within neomutt, looks like this:

\#\<alias\>\<nickname\>\<fullaname\>\<address\>  
alias bob bob jones \<bobjones@fakemail.com\>  
alias joe joe bobs \<joebobs@fakemail.com\>  
alias mimi mimi gator \<mimigator@superfakemail.com\>  
alias sally sally sullen \<ssullen@sallymail.com\>  
alias zeb zeb doolio \<zd@doolmail.com\>  

This is pretty messy and hard to parse at a glance.  So I occasionally open up
the file and run the **column** command to clean it up, yielding this:

<table>
<tr>
<td>#</td><td>\<alias\></td><td>\<nickname\></td><td>\<fullaname\></td><td>\<address\></td>
</tr>
<tr>
<td>alias</td><td>bob</td><td>bob</td><td>jones</td><td><bobjones@fakemail.com></td>
</tr>
<tr>
<td>alias</td><td>joe</td><td>joe</td><td>bobs</td><td><joebobs@fakemail.com></td>
</tr>
<tr>
<td>alias</td><td>mimi</td><td>mimi</td><td>gator</td><td><mimigator@superfakemail.com></td>
</tr>
<tr>
<td>alias</td><td>sally</td><td>sally</td><td>sullen</td><td><ssullen@sallymail.com></td>
</tr>
<tr>
<td>alias</td><td>zeb</td><td>zeb</td><td>doolio</td><td><zd@doolmail.com></td>
</tr>

</table>

Three points to note.  First, the "table" versions of the output above are
rendered using html styling, and don't look as clean as I'd like.  When you run
the **column** command on plain text, it usually ends up looking neater.

Second, you can, of course, pipe the contents of the file directly
through **column** without being in vim.  The following command will do the
trick:

`$ cat mutt_aliases | column -t > mutt_aliases`

Be careful, though, as this will overwrite your file, and you cannot undo the
changes.  That is one reason I like doing it within a vim buffer.  You can
output to a different file, say mutt_aliases_tmp, to eyeball it before replacing
the old file, for some more protection.

And third, a note specific to abook alias files: if your aliases include
nicknames with spaces in them, the outputted, columnar format will likely not be
clean.  **column** will break up the nicknames with spaces in them.

<h3>Conclusion</h3>

The **column** command is a handy way to format text when you're working in vim
or on the terminal.  It is a quick way to clean up messy files, and feels
a little bit like magic when it works.
